import React,{useState} from 'react';
import { Button } from 'antd';
import './Login.css'
import user from '../images/user.png'
import { useHistory } from 'react-router';

var flag = 0;
var nameee;

const Login = () => {
    const [name,setName] = useState('');
    const [psw,setPsw] = useState('');
    const history = useHistory();

    const btn = () => {
        fetch(`https://api.124ss.xyz:2355/endadminlogin?rand=$11`,{
          method: "GET",
          mode: "cors",
          headers: {
            "Content-Type": "text/plain"
          }
    
        }).then(res => {
          return res.json();
        })
        .then(res =>{
            for(var i = 0; i < res.length; i++){
                if(name==res[i]['admin_name'] && psw==res[i]['admin_psw']){
                    flag = 1;
                    localStorage.setItem('admin',name);
                    break;
                }
                else{
                    flag = 0;
                }
            }
            if(flag == 1){
                history.push('/home');
            }
            else{
                alert('用户名或密码错误')
            }
        } )
        .catch((err)=>{
          console.log("err:",err);
        })
    }

    return (
        <div className='container'>
            <div>
                <p>帮您选后台管理系统</p>
            </div>
            <div className='login'>
                <div className='images'>
                    <img src={user} />
                </div>
                <div className='inp'>
                    <input placeholder='用户名' 
                        value = {name}  
                        onChange = {e => {setName(e.target.value)}}
                    />
                </div>
                <div className='inp'>
                    <input placeholder='密码'  
                        type='password'
                        value = {psw} 
                        onChange = {e => {setPsw(e.target.value)}}
                    />
                </div>
                <div className='btn'>
                    <Button onClick={btn}>登录</Button>
                </div>
            </div>
        </div>
    )
}

export default Login;


